<?php
namespace app\index\controller\product;
use app\admin\model\Goods;
class ProductDetails extends \app\index\controller\BaseIndex{
    public function index(){
        $request = \think\Request::instance();
        $goods_id = $request->get('goods_id');
        //根据商品id查询商品表中是否有数据
        $goods = new Goods();
        $item_goods = $goods->where('id',$goods_id)->find();
        if(!$item_goods){
            return $this->error('无效的商品ID！');
        }
        //根据商品的id查询商品的详情图片
        $item_imgs = $item_goods->GoodsPictures;
        //只取出数据库中前四张详情图片
        $item_imgs_front = array_slice($item_imgs, 0,4);
        //取出四张详情图片之后的详情图片
        $item_imgs_behind = array_slice($item_imgs, 4);
        //根据商品的id查询商品的属性
        $item_attr = $item_goods->GoodsAttr;
        //根据商品的id查询商品的类别
        $item_classification = $item_goods->GoodsClassification;

        //做商品推荐
        //根据商品的id查询跟此商品类别一致的4条商品
        //此商品的类别
        $goods_category = $item_classification->category;
        //三表联查
        $rows = \think\Db::table('pc_goods')
            ->alias('a')
            ->join('pc_goods_classification w','a.id = w.goods_id')
            ->join('pc_goods_attr c','a.id = c.goods_id')
            ->where('is_delete','0')
            ->where('status','1')
            ->where('category',$goods_category)
            ->order('w.goods_id desc')
            ->limit(4)
            ->select();
        $data['item_imgs_front'] = $item_imgs_front;
        $data['item_imgs_behind'] = $item_imgs_behind;
        $data['item_goods'] = $item_goods;
        $data['item_attr'] = $item_attr;
        $data['item_classification'] = $item_classification;
        $data['rows'] = $rows;
        return view('product/productDetails',$data);
    }    
}